#ifndef MEMORYCONTEXT_H
#define MEMORYCONTEXT_H

#include "memutils.h"
#include "IAlloc.h"
#include "BBMemPool.h"

class MCPool : public BBMemPool 
{
	public:
		MCPool(const char* pDescription);

		void MemoryContextReset();
		void MemoryContextResetChildren();
		void MemoryContextDelete();
		void MemoryContextDeleteChildren();
		void MemoryContextResetAndDeleteChildren();
		
		void MemoryContextSetParent(MemoryContext new_parent);
		Size GetMemoryChunkSpace(void *pointer);
		MemoryContext GetMemoryChunkContext(void *pointer);
		MemoryContext MemoryContextGetParent();
		bool MemoryContextIsEmpty();
		
		void MemoryContextStats();

#ifdef MEMORY_CONTEXT_CHECKING
		void MemoryContextCheck();
#endif

		bool MemoryContextContains(void *pointer);
		
		void* MemoryContextAlloc(Size size);
		void* MemoryContextAllocZero(Size size);
		void* MemoryContextAllocZeroAligned(Size size);
		~MCPool();
	private:
		void MemoryContextStatsInternal(int level);
		MemoryContext MemoryContextCreate(NodeTag tag, Size size,
					//MemoryContextMethods *methods,
					MemoryContext parent,
					const char *name);
		
	private:
		IAllocSet&	allocset;
		MemoryContext* parent;		/* NULL if no parent (toplevel context) */
		MemoryContext* firstchild;	/* head of linked list of children */
		MemoryContext* nextchild;	/* next child of same parent */
		char	   *name;			/* context name (just for debugging) */
		bool		isReset;		/* T = no space alloced since last reset */

};


#endif

